﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Cci;
using Mutable = Microsoft.Cci.MutableCodeModel;

namespace ReactiveLang.AssemblyRewriter
{
    class ReactiveCodeAssemblyInfo
    {
        public readonly IUnitReference Unit;
        public readonly IUnitNamespaceReference ReactiveCodeNamespace, ObservablesNamespace;
        public readonly ITypeReference VariableType;

        INameTable _nameTable;

        public ReactiveCodeAssemblyInfo(INameTable nameTable)
        {
            _nameTable = nameTable;
            Unit = new Mutable.AssemblyReference
            {
                Location = typeof(IStatement<>).Assembly.Location
            };
            ReactiveCodeNamespace = new Mutable.NestedUnitNamespaceReference
            {
                ContainingUnitNamespace = new Mutable.RootUnitNamespaceReference {  Unit = Unit},
                Name = nameTable.GetNameFor("ReactiveCode")
            };
            ObservablesNamespace = new Mutable.NestedUnitNamespaceReference
            {
                ContainingUnitNamespace = ReactiveCodeNamespace,
                Name = nameTable.GetNameFor("Observables")
            };
            VariableType = new Mutable.NamespaceTypeReference
            {
                ContainingUnitNamespace = ObservablesNamespace,
                Name = nameTable.GetNameFor("Variable"),
                GenericParameterCount = 1
            };
        }

        internal IExpression NewObj(ITypeReference type)
        {
            return new Mutable.CreateObjectInstance
            {
                Type = type,
                MethodToCall = new Mutable.MethodReference { Type = type, Name = _nameTable.GetNameFor(".ctor") }
            };
        }
    }
}
